package com.aigou.admin.aigouspringboot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aigou.admin.aigouspringboot.common.Constants;
import com.aigou.admin.aigouspringboot.context.BaseContext;
import com.aigou.admin.aigouspringboot.entity.ReceivingAddress;
import com.aigou.admin.aigouspringboot.entity.User;
import com.aigou.admin.aigouspringboot.mapper.ReceivingAddressMapper;
import com.aigou.admin.aigouspringboot.mapper.UserMapper;
import com.aigou.admin.aigouspringboot.service.ReceivingAddressService;
import com.aigou.admin.aigouspringboot.service.UserService;
import com.aigou.admin.aigouspringboot.utils.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ReceivingAddressServiceImpl implements ReceivingAddressService {

    @Resource
    private ReceivingAddressMapper receivingAddressMapper;


    @Override
    public List<ReceivingAddress> selectAll() {
        return receivingAddressMapper.selectAll();
    }

    @Override
    public PageInfo<ReceivingAddress> selectReceivingAddressListByPage(Integer current) {
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        // 查询所有用户信息
        List<ReceivingAddress> receivingaddressList = receivingAddressMapper.selectAll();
        // 把userList放入返回分页信息
        return new PageInfo<>(receivingaddressList);
    }

    @Override
    public int deleteById(String id, String token) {

        User user = JSONUtil.toBean(token, User.class);

        // 查询是否为默认
        ReceivingAddress receivingAddress = receivingAddressMapper.selectByPrimaryKey(id);
        // 1 是默认 -1 不是默认
        Integer isDefault = receivingAddress.getIsDefault();
        List<ReceivingAddress> receivingAddresses;
        ReceivingAddress address;
        // 是 查询所有 将第一条修改为默认
        if (isDefault == 1) {
            receivingAddresses = receivingAddressMapper.selectByUserId(user.getUserId());
            // 第一条数据
            address = receivingAddresses.get(0);
            address.setIsDefault(1);
            System.out.println(address.getId().equals(id));
            System.out.println(receivingAddresses.get(receivingAddresses.size() - 1).getId().equals(id));

            // 不是第一条数据也不是最后一条 (中间)
            if (!(address.getId().equals(id)) && !(receivingAddresses.get(receivingAddresses.size() - 1).getId().equals(id))) {
                // 修改第一条为默认地址
                receivingAddressMapper.updateByPrimaryKeySelective(address);
                // 删除地址
                return receivingAddressMapper.deleteByPrimaryKey(id);
            }

            // 第一条或最后一条
            // 是第一条数据
            // 获取第二条数据
            address = receivingAddresses.get(1);
            address.setIsDefault(1);
            // 修改第二条为默认地址
            receivingAddressMapper.updateByPrimaryKeySelective(address);
        }
        // 不是
        return receivingAddressMapper.deleteByPrimaryKey(id);
    }

    @Override

    public ReceivingAddress selectById(String id) {
        return receivingAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(ReceivingAddress receivingAddress) {
        return receivingAddressMapper.updateByPrimaryKeySelective(receivingAddress);
    }

    @Override
    public int save(ReceivingAddress receivingAddress) {
        return receivingAddressMapper.insertSelective(receivingAddress);
    }

    @Override
    public List<ReceivingAddress> selectByUserId(String userId) {
        return receivingAddressMapper.selectByUserId(userId);
    }

    @Override
    public int updateDefaultAddresses(ReceivingAddress receivingAddress) {
        return receivingAddressMapper.insertAddress(receivingAddress);
    }

    @Transactional
    @Override
    public int insertAddress(ReceivingAddress receivingAddress) {
        if (receivingAddress.getIsDefault() == 1) {
            receivingAddressMapper.updateDefaultAddresses(receivingAddress);

        }
        return receivingAddressMapper.insertAddress(receivingAddress);
    }

    @Transactional
    @Override
    public int updateisDefaultAddresses(ReceivingAddress receivingAddress) {
        if (receivingAddress.getIsDefault() == 1) {
            receivingAddressMapper.updateDefaultAddresses(receivingAddress);

        }
        return receivingAddressMapper.updateisDefaultAddresses(receivingAddress);
    }

    @Override
    public void updateIsDefault() {
        receivingAddressMapper.updateIsDefault();
    }
}
